#include "kdcmovieinfomodel.h"

#include <QList>
#include <QDebug>
#include <KLocale>
#include <QTime>

#include <kdclib.h>
#include <kdcmovie.h>
#include <kdcmoviemetadata.h>
#include <kdcvideometadata.h>
#include <kdcaudiometadata.h>


class KDCMovieInfoModelItem
{
public:
	KDCMovieInfoModelItem( const QString &aInfo = QString(), KDCMovieInfoModelItem *aParent = 0 ) : pInfo( aInfo ), pParent( aParent )
	{
		if ( aParent )
			aParent->pChildren.append( this );
	}
	~KDCMovieInfoModelItem()
	{
		clear();
	}
	void clear()
	{
		qDeleteAll( pChildren );
		pChildren.clear();
	}
	int row() const
	{
		if ( pParent )
			return pParent->pChildren.indexOf( const_cast<KDCMovieInfoModelItem*>( this ) );
		return 0;
	}
	QString pInfo;
	QList<KDCMovieInfoModelItem*> pChildren;
	KDCMovieInfoModelItem* pParent;
};

KDCMovieInfoModel::KDCMovieInfoModel( QObject *aParent ) : QAbstractItemModel( aParent ), pRoot( new KDCMovieInfoModelItem )
{

}

KDCMovieInfoModel::~KDCMovieInfoModel()
{
	deletePtr( pRoot );
}

int KDCMovieInfoModel::rowCount( const QModelIndex &aParent ) const
{
	if ( aParent.isValid() )
		return static_cast<KDCMovieInfoModelItem*>( aParent.internalPointer() )->pChildren.size();
	return pRoot->pChildren.size();
}

QVariant KDCMovieInfoModel::data( const QModelIndex &aIndex, int aRole ) const
{
    if ( aIndex.isValid() && aRole == Qt::DisplayRole )
		return static_cast<KDCMovieInfoModelItem*>( aIndex.internalPointer() )->pInfo;
    return QVariant();
}

QModelIndex KDCMovieInfoModel::parent( const QModelIndex &aIndex ) const
{
    if ( !aIndex.isValid() )
        return QModelIndex();

	KDCMovieInfoModelItem *item = static_cast<KDCMovieInfoModelItem*>( aIndex.internalPointer() );
	if ( item && item->pParent != pRoot )
		return createIndex( item->pParent->row(), 0, item->pParent );
	return QModelIndex();
}

int KDCMovieInfoModel::columnCount( const QModelIndex &aParent ) const
{
    return 1;
}

QModelIndex KDCMovieInfoModel::index( int aRow, int aColumn, const QModelIndex &aParent ) const
{
    if ( !hasIndex( aRow, aColumn, aParent ) )
        return QModelIndex();

	KDCMovieInfoModelItem *item = pRoot;
	if ( aParent.isValid() )
		item = static_cast<KDCMovieInfoModelItem*>( aParent.internalPointer() );
	return createIndex( aRow, aColumn, item->pChildren.at( aRow ) );
}

Qt::ItemFlags KDCMovieInfoModel::flags(const QModelIndex &aIndex ) const
{
	return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
}

QVariant KDCMovieInfoModel::headerData( int aSection, Qt::Orientation aOrientation, int aRole ) const
{
    if ( aOrientation == Qt::Horizontal && aRole == Qt::DisplayRole )
        return i18n( "Information about movie(s)" );
    return QAbstractItemModel::headerData( aSection, aOrientation, aRole );
}
#include <QDebug>
void KDCMovieInfoModel::setMovie( KDCMovie *aMovie )
{
	QList<KDCMovie*> movies;
	pRoot->clear();
	if ( aMovie )
	{
		movies.append( aMovie );
		movies.append( aMovie->subMovies() );

		KDCMovieInfoModelItem *item, *tmp;
		KDCAudioMetaData *aInfo;
		KDCVideoMetaData *vInfo;
		for ( int i=0; i<movies.size(); ++i )
		{
			vInfo = movies.at( i )->metaData()->videoMetaData();
			qDebug() << vInfo << movies.at( i )->video()->metaData();
			tmp = new KDCMovieInfoModelItem( fileName( movies.at( i )->metaData()->name() ), pRoot );
			item = new KDCMovieInfoModelItem( i18n("General"), tmp );
			new KDCMovieInfoModelItem( i18n("Length") + " : " + QTime().addSecs( vInfo->length() ).toString( "hh:mm:ss.zzz" ), item );
			
			item = new KDCMovieInfoModelItem( i18n("Video"), tmp );
			new KDCMovieInfoModelItem( i18n("Demuxer") + " : " + toString( vInfo->demuxer() ), item );
			new KDCMovieInfoModelItem( i18n("Codec") + " : " + toString( vInfo->codec() ), item );
			new KDCMovieInfoModelItem( i18n("Width") + " : " + QString::number( vInfo->width() ), item );
			new KDCMovieInfoModelItem( i18n("Height") + " : " + QString::number( vInfo->height() ), item );
			new KDCMovieInfoModelItem( i18n("Bitrate") + " : " + bitrateAsString( vInfo->bitrate() ), item );
			new KDCMovieInfoModelItem( i18n("Fps") + " : " + QString::number( vInfo->fps() ), item );
			new KDCMovieInfoModelItem( i18n("Aspect ratio") + " : " + QString::number( vInfo->aspectRatio() ), item );
			new KDCMovieInfoModelItem( i18n("Frame count") + " : " + QString::number( vInfo->frameCount() ), item );

			for ( int j=0; j<movies.at( i )->metaData()->audioMetaData().size(); ++j )
			{
				aInfo = movies.at( i )->metaData()->audioMetaData().at( j );
				item = new KDCMovieInfoModelItem( i18n("Audio"), tmp );
				new KDCMovieInfoModelItem( i18n("Id") + " : " + QString::number( aInfo->id() ), item );
				new KDCMovieInfoModelItem( i18n("Codec") + " : " + toString( aInfo->codec() ), item );
				new KDCMovieInfoModelItem( i18n("Sample rate") + " : " + QString::number( aInfo->sampleRate() ), item );
				new KDCMovieInfoModelItem( i18n("Bitrate") + " : " + bitrateAsString( aInfo->bitrate() ), item );
				new KDCMovieInfoModelItem( i18n("Channes") + " : " + QString::number( aInfo->channelCount() ), item );
			}
		}
	}
	reset();
}
